package handler

import (
	"context"
	"errors"
	"fmt"
	"time"
	"user-srv/basic/globar"
	__ "user-srv/basic/proto"
	"user-srv/model"
	"user-srv/until"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// 注册模块
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	var u model.User
	u.Mobile = in.Mobile
	u.Password = until.Md5(in.Password)
	if err := u.GetOneFile(globar.DB); err != nil {
		return nil, errors.New("获取参数失败" + err.Error())
	}
	if u.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	if err := u.Create(globar.DB); err != nil {
		return nil, errors.New("添加失败" + err.Error())
	}
	return &__.RegisterResp{
		Id: int64(u.ID),
	}, nil
}

// 登录模块
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var u model.User
	u.Mobile = in.Mobile
	u.Password = in.Password
	if err := u.GetOneFile(globar.DB); err != nil {
		return nil, errors.New("获取参数失败" + err.Error())
	}
	if u.Password != until.Md5(in.Password) {
		return nil, errors.New("密码不一致")
	}
	if u.ID == 0 {
		return nil, errors.New("未获取到此数据")
	}
	return &__.LoginResp{
		Id: int64(u.ID),
	}, nil
}

// 视频发布模块
func (s *Server) Videoadd(_ context.Context, in *__.VideoaddReq) (*__.VideoaddResp, error) {
	var v model.Video
	check := until.Check(in.Title)
	if check == false {
		return nil, errors.New("触发敏感词")
	}
	v = model.Video{
		UserId: int(in.UserId),
		Title:  in.Title,
		Image:  in.Image,
		Info:   in.Info,
		Num:    int(in.Num),
		Types:  int(in.Types),
		Status: int(in.Status),
	}
	globar.Rdb.Set(context.Background(), "title", v, time.Hour)
	if err := v.GetOneFile(globar.DB, in.Title); err != nil {
		return nil, errors.New("获取参数失败" + err.Error())
	}
	if v.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	if err := v.Create(globar.DB); err != nil {
		return nil, errors.New("添加失败" + err.Error())
	}
	return &__.VideoaddResp{
		Id: int64(v.ID),
	}, nil
}

// 视频详情模块
func (s *Server) VideoShow(_ context.Context, in *__.VideoShowReq) (*__.VideoShowResp, error) {
	var v model.Video
	if v.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	if err := v.Show(globar.DB, int(in.Id)); err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	return &__.VideoShowResp{
		UserId: int64(v.UserId),
		Title:  v.Title,
		Image:  v.Image,
		Info:   v.Info,
		Num:    int64(v.Num),
		Types:  int64(v.Types),
		Status: int64(v.Status),
	}, nil
}

// 视频删除模块
func (s *Server) VideoDeleted(_ context.Context, in *__.VideoDeletedReq) (*__.VideoDeletedResp, error) {
	var v model.Video
	if v.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	if err := v.Deleted(globar.DB, int(in.Id)); err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	return &__.VideoDeletedResp{
		Id: in.Id,
	}, nil
}

// 视频修改模块
func (s *Server) VideoUpdate(_ context.Context, in *__.VideoUpdateReq) (*__.VideoUpdateResp, error) {
	var v model.Video
	v = model.Video{
		UserId: int(in.UserId),
		Title:  in.Title,
		Image:  in.Image,
		Info:   in.Info,
		Num:    int(in.Num),
		Types:  int(in.Types),
		Status: int(in.Status),
	}
	if v.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	if err := v.GetUpdate(globar.DB, int(in.Id)); err != nil {
		return nil, errors.New("修改失败" + err.Error())
	}
	return &__.VideoUpdateResp{
		UserId: int64(v.UserId),
		Title:  v.Title,
		Image:  v.Image,
		Info:   v.Info,
		Num:    int64(v.Num),
		Types:  int64(v.Types),
		Status: int64(v.Status),
	}, nil
}

// 视频查询模块
func (s *Server) Videolist(_ context.Context, in *__.VideolistReq) (*__.VideolistResp, error) {
	var v model.Video
	if v.ID != 0 {
		return nil, errors.New("未获取到此数据")
	}
	cover, err := v.GetCover(globar.DB, int(in.Page), int(in.Size))
	fmt.Println(err)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	return &__.VideolistResp{
		List: cover,
	}, nil
}
